package com.wsc.wisdomchina.ai;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.wsc.wisdomchina.R;

public class WebViewActivity extends AppCompatActivity implements View.OnClickListener {

    private WebView mWebView;
    private String url;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        initView();
        initData();
    }

    private void initData() {

        Intent intent = getIntent();
        if (intent != null) {
            url = intent.getStringExtra("url");
            Log.e("Liu", "initData: " + url);
            if (!TextUtils.isEmpty(url)) {
                mWebView.loadUrl(url);
            }
        }
    }

    private void initView() {

        back = ((ImageView) findViewById(R.id.iben_web_back));
        mWebView = ((WebView) findViewById(R.id.mWebView));
        //初始化WebView 的一些设置
        initWebView();

        back.setOnClickListener(this);

    }

    private void initWebView() {
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        // 设置文本编码方式
        settings.setDefaultTextEncodingName("utf-8");
        // 开启缩放 但是不显示缩放工具
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        // 3.0以上才能隐藏缩放工具
        if (android.os.Build.VERSION.SDK_INT >= 11) {
            settings.setDisplayZoomControls(false);
        }

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // 自适应屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iben_web_back:
                finish();
                break;
        }
    }
}
